
var code_err_msg = "输入整数格式类型代码";
var getTariffClassVal = function(){
  var tariff_class_ids = $("#tariff_class_select").val();
  $("#tariff_class_ids").val(tariff_class_ids);
}
$(function() {
  getTariffClassVal();

  /*表单验证：begin*/
  var $myField = null;
  var myMsg = "";
  $("#frm").validator({
    // 域通过验证时回调
    onValid: function (validity) {
      $(validity.field).closest('.am-form-group').find('.am-alert').hide();
    },
    // 验证出错时的回调， validity 对象包含相关信息，格式通 H5 表单元素的 validity 属性
    onInValid: function (validity) {
      var $field = $(validity.field);
      var field_id = $field.attr('id');

      var $group = $field.closest('.am-form-group');
      var $alert = $group.find('.am-alert');
      var msg = "";
      if ($myField && myMsg && $field.val() && $field.is($myField)) {
        msg = myMsg;
        $myField = null;
        myMsg = "";
      }else if(field_id=="type"){
        msg = code_err_msg;
      }else {
        // 使用自定义的提示信息 或 插件内置的提示信息
        msg = $field.data('validationMessage') || this.getValidationMessage(validity);
      }
      if (!$alert.length) {
        $alert = $("<div class='am-alert am-alert-danger'></div>").hide().
            appendTo($group.find(".input-msg"));
      }
      console.log("onInValid : " + $field.val());
      $alert.html(msg).show();
    },
    //自定义验证
    validate: function (validity) {
      if ($(validity.field).is('#name')) {
        var name = $("#name").val().trim();
        if(!name || name.length<2){
          validity.valid = false;
        }
      };


      /**
       * 校验地市资费代码的唯一性
       */
      var validityCodeUnique = function(){
        var type = $("#type").val();
        var activityId = $("#activityId").val();
        var params = {type:type,activityId:activityId};
        var reg = new RegExp("^[0-9]*$");
        if(type && type.trim().length>0 && reg.test(type)){
          $.ajax({
            type: "post",
            url: "../activity/validityTypeUnique",
            data: params,
            dataType: "json",
            async:false,
            success: function(data) {
              if(data.resultCode != 1000){
                code_err_msg = "类型代码已存在";
                validity.valid = false;
              }else{
                validity.valid = true;
              }
            },
            error : function(textStatus) {
              validity.valid = false;
            }
          });
        }else{
          code_err_msg = "输入整数格式类型代码";
          validity.valid = false;
        }
      };
      if ($(validity.field).is('#type')) {
        //validityCodeUnique();
      };
    },
    submit: function () {
      var formValidity = this.isFormValid();
      // 表单验证成功，弹出确认提示
      if (formValidity) {
        return true;
      }
      console.warn('验证状态：', formValidity ? '通过' : '未通过');
      return false;
    }
  });






  /*附件上传：begin*/
  var jqXHR = $('#fileupload').fileupload({
    url: base+"/upload/image/",
    dataType: 'json',
    start: function (e) {
      $("#progress-area").removeClass("am-hide");
      $("#progress-text").removeClass("am-text-danger");
      $("#progress-text").html("");
      $(".am-progress-bar").css("width","0%");
    },
    done: function (e, data) {
      console.log(data);
      $("#icon_url").val(data.result.url);
      setTimeout(function() {
        $("#progress-area").addClass("am-hide");
      },1500);
    },
    progressall: function (e, data) {
      var progress = parseInt(data.loaded / data.total * 100, 10);
      $(".am-progress-bar").css("width",progress + "%");
      $("#progress-text").html(progress + "%");
    },
    error: function (jqXHR, textStatus, errorThrown) {
      console.log("imageupload error！");
      // console.log(jqXHR);
      // console.log(textStatus);
      // console.log(errorThrown);
      $("#progress-area").removeClass("am-hide");
      $("#progress-text").addClass("am-text-danger");
      $("#progress-text").html("imageupload error！");
      $(".am-progress-bar").css("width","0%");
      setTimeout(function() {
        $("#progress-area").addClass("am-hide");
      },2000);
    },
    fail: function (jqXHR, textStatus) {
      console.log("imageupload fail！");
      // console.log(jqXHR);
      // console.log(textStatus);
      $("#progress-area").removeClass("am-hide");
      $("#progress-text").addClass("am-text-danger");
      $("#progress-text").html("imageupload fail！");
      $(".am-progress-bar").css("width","0%");
      setTimeout(function() {
        $("#progress-area").addClass("am-hide");
      },2000);
    }
  });
  /*附件上传：end*/















});
